package com.byzk.p2p.admin.borrow.controller;

import java.net.URLDecoder;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.dto.Borrow;
import com.byzk.p2p.admin.borrow.dto.UserTender;
import com.byzk.p2p.admin.borrow.service.BorrowService;
import com.byzk.p2p.admin.borrow.service.TenderService;
import com.byzk.p2p.admin.borrow.util.BorrowStatus;
import com.byzk.p2p.admin.borrow.vo.UserTenderVo;

/**
 * 投资业务相关
 * @author suker.zhou
 *
 */
@Controller
@RequestMapping("/admin/tender")
public class TenderController extends AdminBaseController {

	@Autowired
	private TenderService tenderService;
	@Autowired
	private BorrowService borrowService;
	
	/**
	 * 满标复审
	 * @param tid
	 * @return
	 */
	@RequestMapping("/compelete")
	public String completeTender(@RequestParam("tid")Integer tid){
		try {
			Borrow borrow=borrowService.detailBorrow(tid);
			if(BorrowStatus.PAY.equals(borrow.getStatus())){
				super.printJSON(new JsonMsg(0, "不能重复操作"));
				return null;
			}
			//tenderService.compeleteBorrow(borrow);
			super.printJSON(new JsonMsg(-1, "操作成功"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 新手标满标复审
	 * @param tid
	 * @return
	 */
	@RequestMapping("/completeTenderNewBorrow")
	public String completeTenderNewBorrow(@RequestParam("tid")Integer tid){
		try {
			Borrow borrow=borrowService.detailBorrow(tid);
			if(BorrowStatus.PAY.equals(borrow.getStatus())){
				super.printJSON(new JsonMsg(0, "不能重复操作"));
				return null;
			}
			tenderService.compeleteNewBorrow(borrow);
			super.printJSON(new JsonMsg(-1, "操作成功"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 债权转让
	 * @param bid 标id
	 * @param uid 转让后用户id
	 * @return
	 */
	public String changeBorrow(@RequestParam("bid")Integer bid,
							   @RequestParam("uid")Integer uid){
		
		return null;
	}
	
	/**
	 * 获取用户投资记录
	 * @param model
	 * @param index
	 * @param limit
	 * @param uid 用户id
	 * @return
	 */
	@RequestMapping(value="/listUserTender")
	public String listUserTender(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit,
			   @RequestParam Integer uid){
		UserTender userTender = new UserTender();
		userTender.setUserId(uid);
		userTender.setIndex(index);
		userTender.setLimit(limit);
		PageInfo<UserTenderVo> pageInfo = tenderService.listAll(userTender);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("uid", uid);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 投资列表
	 * @param model
	 * @param index 当前页
	 * @param limit 每页条数
	 * @param status 借款标状态
	 * @param matchInviteUser 邀请人
	 * @param timeBegin 投标时间，开始
	 * @param timeEnd 投标时间，结束
	 * @param borrowCode 借款标编号
	 * @param tenderUserName 投资用户名或手机号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public String listTender(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit,
			   @RequestParam(value="status",required=false)String status,
			   @RequestParam(value="matchInviteUser",required=false)String matchInviteUser,
			   @RequestParam(value="timeBegin",required=false)String timeBegin,
			   @RequestParam(value="timeEnd",required=false)String timeEnd,
			   @RequestParam(value="borrowCode",required=false)String borrowCode,
			   @RequestParam(value="tenderUserName",required=false)String tenderUserName) throws Exception{
		UserTenderVo userTenderVo = new UserTenderVo();
		userTenderVo.setIndex(index);
		userTenderVo.setLimit(limit);
		if (StringUtils.isNotBlank(status)) {
			userTenderVo.setStatus(status);
			model.addAttribute("status", status);
		}
		if (StringUtils.isNotBlank(matchInviteUser)) {
			matchInviteUser=java.net.URLDecoder.decode(matchInviteUser , "UTF-8");
			userTenderVo.setMatchInviteUser(matchInviteUser);
			model.addAttribute("matchInviteUser", matchInviteUser);
		}
		if (StringUtils.isNotBlank(timeBegin)) {
			userTenderVo.setCreateTimeBegin(DateUtils.getRealDate(DateUtils.convert(timeBegin, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("timeBegin", timeBegin);
		}
		if (StringUtils.isNotBlank(timeEnd)) {
			userTenderVo.setCreateTimeEnd(DateUtils.getFullDate(DateUtils.convert(timeEnd, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("timeEnd", timeEnd);
		}
		if (StringUtils.isNotBlank(borrowCode)) {
			userTenderVo.setBorrowCode(borrowCode);
			model.addAttribute("borrowCode", borrowCode);
		}
		if (StringUtils.isNotBlank(tenderUserName)) {
			tenderUserName=URLDecoder.decode(tenderUserName,"UTF-8");
			userTenderVo.setMatchUser(tenderUserName);
			model.addAttribute("tenderUserName", tenderUserName);
		}
		PageInfo<UserTenderVo> pageInfo = tenderService.myTender(userTenderVo);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
}
